package com.tzq.SERVLET;

import java.io.IOException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.jasper.tagplugins.jstl.core.Out;

import com.tzq.JAVABEAN.ConnectDB;

/**
 * Servlet implementation class Comment
 */
public class Comment extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Comment() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int method  = Integer.parseInt(request.getParameter("method"));
		if(method==1){
			try {
				BlogComment(request, response);
			} catch (SQLException e) {
				response.getWriter().print("no");
				e.printStackTrace();
			}
		}
		if(method==2){
			try {
				VideoComment(request, response);
			} catch (SQLException e) {
				response.getWriter().print("no");
				e.printStackTrace();
			}
		}
	}
	
	private void VideoComment(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException{
		SimpleDateFormat dff = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String videoIDString  =request.getParameter("VideoID");
		String commenteridString = request.getParameter("commenterid");
		if(commenteridString.equals("null")){
			commenteridString = "5000032";
		}
		String nowDate = dff.format(new Date());
		String commenttextString = request.getParameter("commenttext");
		String sql = "insert into VideoComment values(?,?,?,?)";
		String[] SqlParam ={videoIDString,commenteridString,nowDate,commenttextString}; 
		int[] WhichInt = {1,1,0,0};
		ConnectDB connectDB = new ConnectDB();
		boolean flag = connectDB.updatesql(sql, SqlParam, 4, WhichInt);
		if(flag){
			response.getWriter().print("yes");
		}else{
			response.getWriter().print("no");
		}

	}
	
	private void BlogComment(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException{
		SimpleDateFormat dff = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String blognameString = request.getParameter("blogname");
		String authoridString = request.getParameter("authorid");
		String commenteridString = request.getParameter("commenterid");
		if(commenteridString.equals("null")){
			commenteridString = "5000032";
		}
		System.out.println(commenteridString);
		String nowDate = dff.format(new Date());
		String commenttextString = request.getParameter("commenttext");
		String sql = "insert into BlogComment values(?,?,?,?,?)";
		String[] SqlParam ={blognameString,authoridString,commenteridString,nowDate,commenttextString}; 
		int[] WhichInt = {0,1,1,0,0};
		ConnectDB connectDB = new ConnectDB();
		boolean flag = connectDB.updatesql(sql, SqlParam, 5, WhichInt);
		if(flag){
			response.getWriter().print("yes");
		}else{
			response.getWriter().print("no");
		}
	}

}
